<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Http;
use app\api\model\Goods;
use think\Db;

/**
 * 订单
 */
class Order extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 下单信息
     * @param $goods_id 商品ID
     * @param $num 数量
     * @ApiMethod (GET)
     */
    public function info()
    {
        $goods_id = input('goods_id');
        $num = input('num');

        $goods_info = Goods::where('id', $goods_id)->find();
        if(!$goods_info)
        {
            $this->error('参数错误');
        }

        if(!$num)
        {
            $this->error('请选择正确的数量');
        }

        $data['info'] = $goods_info;
        $data['num'] = $num;
        $data['expressage'] = '随机';

        //判断是否是会员
        $vip = Db::table('user_vip')->where('user_id', $this->auth->id)->find();
        if($vip)
        {
            //总价
            $total_money = $goods_info['vip_money'] * $num;
            //原价
            //
            $old_total_money = $goods_info['money'] * $num;
            //优惠价
            $discounts_money = $goods_info['money'] * $num - $goods_info['vip_money'] * $num;
        }else{
            //总价
            $total_money = $goods_info['money'] * $num;
            //原价
            $old_total_money = $goods_info['money'] * $num;
            //优惠价
            $discounts_money = 0;
        }

        //判断路费
        if($goods_info['freight'] == '免运费' || $goods_info['freight'] == 0)
        {
            $freight = 0;
        }else{
            $freight = $goods_info['freight'];
        }

        $data['total_money'] = $total_money + $freight;
        $data['old_total_money'] = $old_total_money;
        $data['discounts_money'] = $discounts_money;
        $data['freight'] = $freight;

        $data['address'] = $this->address();
        $this->success('OK', $data);
    }

    /**
     * 订单收货地址
     */
    public function address()
    {
        $address = Db::table('user_address')->where('user_id', $this->auth->id)->order('is_moren', 'desc')->select();
        return $address;
    }
}
